package com.offcn.service.impl;

import com.alibaba.fastjson.JSON;
import com.offcn.sellergood.entity.GoodsEntity;
import com.offcn.sellergood.feign.GoodsFeign;
import com.offcn.sellergood.feign.ItemCatFeign;
import com.offcn.sellergood.pojo.Goods;
import com.offcn.sellergood.pojo.GoodsDesc;
import com.offcn.sellergood.pojo.Item;
import com.offcn.sellergood.pojo.ItemCat;
import com.offcn.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zola
 * @Date: 2021/9/14 - 09 - 14 - 20:20
 * @Description: com.offcn.service.impl
 * @Version: 1.0
 */
@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private ItemCatFeign itemCatFeign;

    @Value("${pagePath}")
    private String pagePath;

    @Autowired
    private TemplateEngine templateEngine;  // thymeleaf的模板引擎
    /**
     * @param: Long goodsId
     * @return: Map<String, Object>
     * @description: 封装一个方法，来将查询到的页面数据返回
     * @analyze: 通过将远程注入的接口所提供的方法来实现获取
     */
    private Map<String, Object> getData(Long goodsId){
        Map<String, Object> map = new HashMap<>();  // 定义一个空容器，用户将获取的数据存入
        GoodsEntity goodEntity = goodsFeign.findById(goodsId).getData();
        // 先获取三个实体类，并放入map容器中
        Goods tbGoods = goodEntity.getTbGoods();
        GoodsDesc tbGoodsDesc = goodEntity.getTbGoodsDesc();
        List<Item> itemList = goodEntity.getItemList();
        map.put("tbGoods",tbGoods);
        map.put("tbGoodsDesc",tbGoodsDesc);
        map.put("itemList",itemList);
        // 再用户处理其中属性中类型为 json格式的
        List<Map> imageList = JSON.parseArray(tbGoodsDesc.getItemImages(), Map.class);
        map.put("images",imageList);
        List<Map> specList = JSON.parseArray(tbGoodsDesc.getSpecificationItems(), Map.class);
        map.put("specs",specList);
        // 最后，设置三级标题
        ItemCat itemCat1 = itemCatFeign.findById(tbGoods.getCategory1Id()).getData();
        ItemCat itemCat2 = itemCatFeign.findById(tbGoods.getCategory2Id()).getData();
        ItemCat itemCat3 = itemCatFeign.findById(tbGoods.getCategory3Id()).getData();
        map.put("category1",itemCat1);
        map.put("category2",itemCat2);
        map.put("category3",itemCat3);
        // 返回结果
        return map;
    }

    /**
     * @param:
     * @return:
     * @description: 用于将从数据库中查询的结果数据存入到文件中，并导出
     * @analyze:
     */
    @Override
    public void createPageHtml(Long goodsId) throws FileNotFoundException, UnsupportedEncodingException {
        // 1.获取数据
        Map<String, Object> map = getData(goodsId);
        // 2.获取(thymeleaf)上下文环境对象、将获取到的数据存入
        Context context = new Context();
        context.setVariables(map);
        // 3.根据自定义的路径生成目录及其文件
        File directory = new File(pagePath);
        if (!directory.exists()) {
            directory.mkdirs(); // 生成多级目录
        }
        // 创建文件
        File file = new File(directory, goodsId + ".html");
        // 4.利用模板引擎生成文件
        Writer writer = new PrintWriter(file,"UTF-8");
        templateEngine.process("item",context,writer);
    }
}
